package star.oath.dataset.special.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import star.oath.dataset.special.model.Special;
import star.oath.dataset.special.service.SpecialService;
import star.oath.framework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    // 查询 yu
    @GetMapping("/yu")
    public List<String> getYu() {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT yu")
                .eq("level", 1)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 yu 查询 jie
    @GetMapping("/jie")
    public List<String> getJieByYu(@RequestParam String yu) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT jie")
                .eq("level", 2)
                .eq("yu", yu)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 jie 查询 men
    @GetMapping("/men")
    public List<String> getMenByJie(@RequestParam String jie) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT men")
                .eq("level", 3)
                .eq("jie", jie)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 men 查询 gang
    @GetMapping("/gang")
    public List<String> getGangByMen(@RequestParam String men) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT gang")
                .eq("level", 4)
                .eq("men", men)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 gang 查询 mu
    @GetMapping("/mu")
    public List<String> getMuByGang(@RequestParam String gang) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT mu")
                .eq("level", 5)
                .eq("gang", gang)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 mu 查询 ke
    @GetMapping("/ke")
    public List<String> getKeByMu(@RequestParam String mu) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT ke")
                .eq("level", 6)
                .eq("mu", mu)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }
    // 根据 ke 查询 shu
    @GetMapping("/shu")
    public List<String> getShuByKe(@RequestParam String ke) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT shu")
                .eq("level", 7)
                .eq("ke", ke)
                .orderByAsc("name");
        List<Special> specialList = specialService.list(wrapper);
        List<String> list = specialList.stream()
                .map(Special::getJie)
                .collect(Collectors.toList());
        return list;
    }

    /**
     * 根据条件查询
     * @param params
     * @return
     */
    @PostMapping("/list")
    public Page<Special> listByConditions(@RequestBody Map<String, Object> params) {
        QueryWrapper<Special> wrapper = new QueryWrapper<>();
        String yu = (String) params.get("yu");
        if (StringUtils.isNotEmpty(yu)) {
            wrapper.eq("yu", yu);
        }
        String jie = (String) params.get("jie");
        if (StringUtils.isNotEmpty(jie)) {
            wrapper.eq("jie", jie);
        }
        String men = (String) params.get("men");
        if (StringUtils.isNotEmpty(men)) {
            wrapper.eq("men", men);
        }
        String gang = (String) params.get("gang");
        if (StringUtils.isNotEmpty(gang)) {
            wrapper.eq("gang", gang);
        }
        String mu = (String) params.get("mu");
        if (StringUtils.isNotEmpty(mu)) {
            wrapper.eq("mu", mu);
        }
        String ke = (String) params.get("ke");
        if (StringUtils.isNotEmpty(ke)) {
            wrapper.eq("ke", ke);
        }
        String shu = (String) params.get("shu");
        if (StringUtils.isNotEmpty(shu)) {
            wrapper.eq("shu", shu);
        }
        String name = (String) params.get("name");
        if (StringUtils.isNotEmpty(name)) {
            wrapper.eq("name", name);
        }
        if (params.get("level") != null) {
            Integer level = Integer.parseInt(String.valueOf(params.get("level")));
            wrapper.eq("level", level);
        }
        wrapper.orderByAsc("yu","jie","men","gang","mu","ke","shu","zhong","level","name");
        Integer pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        Integer pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        Page<Special> specialPage = specialService.page(new Page<>(pageNum,pageSize),wrapper);
        for (int i = 0; i < specialPage.getRecords().size(); i++) {
            Special special = specialPage.getRecords().get(i);
            StringBuffer fenlei = new StringBuffer();
            if (StringUtils.isNotEmpty(special.getYu())) {
                fenlei.append(special.getYu());
            }
            if (StringUtils.isNotEmpty(special.getJie())) {
                fenlei.append("-").append(special.getJie());
            }
            if (StringUtils.isNotEmpty(special.getMen())) {
                fenlei.append("-").append(special.getMen());
            }
            if (StringUtils.isNotEmpty(special.getGang())) {
                fenlei.append("-").append(special.getGang());
            }
            if (StringUtils.isNotEmpty(special.getMu())) {
                fenlei.append("-").append(special.getMu());
            }
            if (StringUtils.isNotEmpty(special.getKe())) {
                fenlei.append("-").append(special.getKe());
            }
            if (StringUtils.isNotEmpty(special.getShu())) {
                fenlei.append("-").append(special.getShu());
            }
            if (StringUtils.isNotEmpty(special.getZhong())) {
                fenlei.append("-").append(special.getZhong());
            }
            special.setFenlei(fenlei.toString());
        }
        return specialPage;
    }

}